package com.bxd.baodan.fragment;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * 向导
 * @author syq
 * 
 *		Create on: 2014年9月10日
 */
public class GuideFragment2 extends DialogFragment{

	private static GuideFragment2 guideFragment;
	private NoScrollViewPager pager;

	public static GuideFragment2 newInstance() {
		guideFragment = new GuideFragment2();
		return guideFragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(DialogFragment.STYLE_NORMAL,
				android.R.style.Theme_Black_NoTitleBar_Fullscreen);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.view_pager_no_scroll, container, false);
		pager = (NoScrollViewPager) view.findViewById(R.id.pager);
		pager.setNoScroll(true);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle arg0) {
		super.onActivityCreated(arg0);
		pager.setAdapter(new PagerAdapter() {
			int[] resIds = new int[] {/* R.drawable.img_1, R.drawable.img_2,
					R.drawable.img_3,R.drawable.img_4,R.drawable.img_5*/};
			List<View> viewList = new ArrayList<View>();

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				return resIds.length;
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				container.removeView(viewList.get(position));

			}

			@Override
			public Object instantiateItem(ViewGroup container, final int position) {
				View view = LayoutInflater.from(App.getContext()).inflate(
						R.layout.guide_image, null);
				ImageView guideImage = (ImageView) view
						.findViewById(R.id.guide_image);
				guideImage.setImageResource(resIds[position]);
				guideImage.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (position == resIds.length - 1) {
							dismiss();
						}
						try {
							pager.setCurrentItem(position + 1);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

				});
				viewList.add(view);
				container.addView(view);
				// return viewList.get(position);
				return view;
			}
		});
	}
}
